package org.ntu.csn.doraemon.canvas;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import org.ntu.csn.doraemon.data.RMSUtil;
import org.ntu.csn.doraemon.model.Mark;

public class RankingForm implements CommandListener {


	public Form rankingForm = new Form("Ranking");	
	
	private StartMenu sm ;	
	
	public RankingForm(StartMenu sm) {
		this.sm = sm;
		addForm();
	}

	public void addForm() {		

		rankingForm.append("Game Ranking");    	
    	  
		StringItem item = new StringItem("Ranking", "Doraemon");
		rankingForm.append(item);
		
		Vector dataSet = RMSUtil.listObjects(Mark.class,0, RMSUtil.getStoreSize(Mark.class));
    	if(dataSet == null)  {    	
    		item = new StringItem("list is empty" , " : no data");
  		  	rankingForm.append(item);
    	} else {
		Enumeration e = dataSet.elements();
    	Mark m = new Mark();
    	while(e.hasMoreElements()){
    		  m = (Mark)e.nextElement();
    		  System.out.println("Name: "+m.getName()+" Mark:"+m.getMark()+" Id:"+m.getId());
    		  item = new StringItem("Name: "+m.getName(), " Mark:"+m.getMark());
    		  rankingForm.append(item);
    	  }
    	  
    	 System.out.println("Size of Mark Record Store:"+RMSUtil.getStoreSize(Mark.class));
    	}
		
		rankingForm.addCommand(Listener.back);
		rankingForm.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {
		if ( c == Listener.back) {
			 Display.getDisplay(sm).setCurrent(StartMenu.mainMenu);
        }
	}

	public Form getRankingForm() {
		return rankingForm;
	}

	public void setRankingForm(Form rankingForm) {
		this.rankingForm = rankingForm;
	}

	
}
